﻿using System;
using System.ServiceModel;
using LB.Common;
using LB.Server.Common;
using DBEntites = LB.Server.DAL.DomainEntitites;

namespace LB.Server.Core
{
    //ToDo: what is it?
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class UserAdministrationService : IUserAdministrationService
    {
        private readonly IUserManagement userManagement;
        private readonly ApplicationLog log = new ApplicationLog();

        public UserAdministrationService(){}
         
        public UserAdministrationService(IUserManagement userManagement)
        {
            log.WriteInfo("Invoke UserAdministration constructor.");
            this.userManagement = userManagement;
        }

        #region IUserAdministrationService Members

        public LoginToken Login(string login, string hash)
        {
            log.WriteTrace("Try to login.\t user name - {0}, password - {1}", login, hash);
            var result = userManagement.Login(login, hash);
            log.WriteTrace("Login result Token - {0}", result.SessionId);
            return result;
        }

        public void Logout(LoginToken loginToken)
        {
            log.WriteInfo("Logout. Session Id - {0}", loginToken.SessionId);
            userManagement.LogOut(loginToken.SessionId);
        }

        public bool VerifyLogin(LoginToken loginToken)
        {
            return userManagement.IsSessionActive(loginToken.SessionId);
        }

        public User GetCurrentUserInfo(LoginToken loginToken)
        {
            // ToDo: Implement
            throw new NotImplementedException();
        }

        public bool CreateUser(string login, string password)
        {
            return userManagement.CreateAccount(login, password);
        }

        public bool IsLoginExist(string login)
        {
            return userManagement.IsLoginExist(login);
        }

        #endregion

        public DateTime Ping()
        {
            return DateTime.UtcNow;
        }
    }
}